package com.library.system.controller;

import com.library.common.annotation.CurrentUser;
import com.library.common.enums.RoleTypeEnum;
import com.library.common.service.bo.CurrentLoginUser;
import com.library.logging.annotation.LogSys;
import com.library.system.modules.notice.vo.NoticeVO;
import com.library.system.modules.notice.bo.NoticePage;
import com.library.system.modules.notice.bo.NoticeInsert;
import com.library.system.modules.notice.service.NoticeService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.List;


/**
 * @Author xyh
 * @Desc 通知公告表(Notice)表控制层
 * @Date 2023-11-04 17:14:08
 */

@RestController
@RequestMapping("notice")
public class NoticeController {

    @Resource
    private NoticeService noticeService;


    /**
     * 分页查询列表
     *
     * @return 数据
     */
    @LogSys(value = "通知公告分页查询列表")
    @GetMapping("/list")
    public Result<IPage<NoticeVO>> queryByPage(@CurrentUser CurrentLoginUser currentLoginUser, @Valid NoticePage page) {
        List<Integer> roleIds = currentLoginUser.getRoleIds();
        if (roleIds.contains(RoleTypeEnum.SUPER_ADMIN.getCode()) || roleIds.contains(RoleTypeEnum.LIBRARY_ADMIN.getCode())) {
            page.setUserId(null);
        } else {
            page.setUserId(currentLoginUser.getUserId());
        }
        return Result.success(noticeService.queryByPage(page));
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个通知公告表数据
     */
    @LogSys(value = "单个通知公告表数据")
    @PostMapping("/queryById/{id}")
    public Result<NoticeVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(noticeService.queryById(id));
    }


    /**
     * 添加通知公告表
     *
     * @return 新增通知公告表数据
     */
    @LogSys(value = "新增通知公告表数据")
    @PostMapping("/insert")
    public Result insert(@Valid @RequestBody NoticeInsert param, @CurrentUser CurrentLoginUser currentLoginUser) {
        String s = checkParam(param);
        if (s != null) {
            return Result.error(s);
        }
        param.setUserName(currentLoginUser.getUsername());
        param.setUserId(currentLoginUser.getUserId());
        noticeService.insertOrUpdate(param);
        return Result.success();
    }

    private String checkParam(NoticeInsert insert) {
        if (insert.getSendTime() != null) {
            LocalDateTime currentTime = LocalDateTime.now();
            Duration duration = Duration.between(currentTime, insert.getSendTime());
            if (duration.toMinutes() < 3) {
                return "当前选择的发布时间在10分钟以内，不能定时发布，请重新修改发布时间！";
            }
        }
        return null;
    }

    /**
     * 删除通知公告表
     *
     * @return 删除通知公告表数据
     */
    @LogSys(value = "删除通知公告表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        noticeService.deleteById(id);
        return Result.success();
    }

    /**
     * 取消定时发布公告
     *
     * @param id
     * @return
     */
    @LogSys(value = "取消定时发布公告")
    @PostMapping("/cancel/{id}")
    public Result cancelNoticeTime(@PathVariable("id") Integer id) {
        NoticeVO vo = noticeService.queryById(id);
        if (vo != null) {
            LocalDateTime currentTile = LocalDateTime.now();
            if (currentTile.isAfter(vo.getSendTime().minusMinutes(3))) {
                return Result.error("当前公告定时任务已不支持取消！");
            }
        }
        noticeService.cancelNoticeTime(id);
        return Result.success();
    }

}
